<template>
  <div class="login-box">
    <!-- 顶部导航 -->
    <van-nav-bar class="page-nav-bar" title="登录">
       <template #left>
        <van-icon @click="$router.go(-1)" name="cross" size="18" />
      </template>
    </van-nav-bar>
    <!-- 表单区域 -->
    <van-form ref="userForm" @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="username"
        placeholder="请输入手机号"
        :rules="userRules.mobile"
        type="number"
        maxlength="11"
      >
      <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="password"
        placeholder="请输入验证码"
        :rules="userRules.code"
        type="number"
        maxlength="6"
      >
      <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
      <template #button>
        <van-count-down @finish="showFlag = false" v-if="showFlag" :time="60000" format="剩余 ss s" />
        <van-button v-else @click="sendCode" class="send-btn" size="small" type="default" native-type="button">发送验证码</van-button>
      </template>
      </van-field>
      <div class="login-btn">
        <van-button class="btn" block type="info" native-type="submit">登 录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, sendCode } from '@/api/user'
export default {
  name: 'Login',
  data () {
    return {
      user: {
        mobile: '',
        code: ''
        // mobile: '13911111111',
        // code: '246810'
      },
      userRules: {
        mobile: [
          { required: true, message: '手机号码不能为空' },
          { pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/, message: '手机号码格式不正确' }
        ],
        code: [
          { required: true, message: '手机号码不能为空' },
          { pattern: /^\d{6}$/, message: '验证码格式不正确' }
        ]
      },
      showFlag: false
    }
  },

  created () {

  },

  methods: {
    async onSubmit () {
      console.log(this.user)
      this.$toast.loading({
        message: '登录中...',
        forbidClick: true,
        duration: 0
      })
      try {
        const { data: res } = await login(this.user)
        console.log(res)
        this.$store.commit('setUser', res.data)
        this.$toast.success('登录成功')
        this.$router.go(-1)
      } catch (e) {
        console.dir(e)
        if (e.response.status === 400) {
          this.$toast.fail('手机号或验证码不正确')
        } else {
          this.$toast.fail('系统异常')
        }
      }
    },
    async sendCode () {
      try {
        await this.$refs.userForm.validate('username')
        console.log('验证成功')
      } catch (e) {
        console.log('验证失败')
        return
      }
      this.showFlag = true
      try {
        const res = await sendCode(this.user.mobile)
        console.log(res)
      } catch (e) {
        console.dir(e)
        this.showFlag = false
        if (e.response.status === 429) return this.$toast('操作频繁')
        return this.$toast('系统错误')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.toutiao {
  font-size: 37px;
}
.send-btn {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
    border-radius: 23px;
}
.login-btn {
  margin: 53px 28px;
  .btn {
    background-color: #6db4fb;
    border: none;
  }
}
.van-icon {
  color: #fff;
}
</style>
